package cn.wolfcode.ssm.web.controller;


import cn.wolfcode.ssm.domain.Consumption;
import cn.wolfcode.ssm.enums.ConsumptionStatusEnum;
import cn.wolfcode.ssm.service.IConsumptionItemService;
import cn.wolfcode.ssm.service.IConsumptionService;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IConsumptionItemService consumptionItemService;


    @RequireAnnotation(name = "结算单页面",expression = "consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("enums", ConsumptionStatusEnum.values());
        return "consumption/list";
    }

    @RequestMapping("/delete")
    @RequireAnnotation(name = "结算单删除",expression = "consumption:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            consumptionService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequireAnnotation(name = "结算单新增/编辑",expression = "consumption:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Consumption consumption){
        if (consumption.getId() != null) {
            consumptionService.update(consumption);
        }else {
            consumptionService.save(consumption);
        }
        return new JsonResult();
    }

    @RequestMapping("/save")
    @RequireAnnotation(name = "结算单新增/编辑",expression = "consumption:saveOrUpdate")
    @ResponseBody
    public JsonResult save(String ano,Integer status){
        Consumption consumption = consumptionService.save(ano, status);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(consumption);
        return jsonResult;
    }

    @RequestMapping("/input")
    @RequireAnnotation(name = "结算单新增/编辑",expression = "consumption:input")
    public String input(Model model,Long id){


        Consumption consumption = consumptionService.get(id);
        model.addAttribute("consumption",consumption);
        model.addAttribute("consumptionItem",consumptionItemService.selectByCno(consumption.getCno()));
        return "consumption/input";
    }
}
